﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rInvoice.Utils;

namespace rInvoice
{
    public partial class ContractsInputPanel : Form
    {
        private ServerObject mServerObject = null;

        string mContractName = string.Empty;
        string mDescription = string.Empty;
        int mCompanyID = 0;
        int mContractID = 0;
        bool mIsDeleted = false;

        bool mCreatingNew = false;
        bool mIsLoading = false;


        public bool CreateNew
        {
            get {  return mCreatingNew; }
            set {  mCreatingNew = value; }
        }

        public int CompanyID
        {
            get { return mCompanyID; }
            set { mCompanyID = value; }
        }

        public int ContractID
        {
            get { return mContractID; }
            set { mContractID = value; }
        }

        public string ContractName 
        {
            get { return mContractName; }
            set { mContractName = value; }
        }

        public string Description 
        {
            get { return mDescription; }
            set { mDescription = value; }
        }

        public ContractsInputPanel()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void ContractsInputPanel_Load(object sender, EventArgs e)
        {
            mIsLoading = true;
           
            FillCompanyComboBox();

            if (!mCreatingNew)
            {
                companyComboBox.SelectedValue = mCompanyID;
                contractNameTextBox.Text = mContractName;
                contractDescriptionTextBox.Text = mDescription;
                activeChekBox.Checked = !mIsDeleted;
            }

            mIsLoading = false;

        }

        private void FillCompanyComboBox()
        {
            DataTable gr1 = mServerObject.SelectCompanyList(false);
            if (gr1.Rows.Count > 0)
            {
                companyComboBox.DataSource = gr1;
                companyComboBox.DisplayMember = "CompanyName";
                companyComboBox.ValueMember = "CompanyID";
                companyComboBox.SelectedIndex = -1;
            }
            else
            {
                companyComboBox.Text = "For this classifier enter the required data.";
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            SetEdit();
        }

        private void SetEdit()
        {
            bool result = false;

            if (mCreatingNew)
            {
                result = mServerObject.AddContract(mCompanyID, mContractName, mDescription);
            }
            else
            {
                result = mServerObject.UpdateContract(mContractID,mCompanyID, mContractName, mDescription,mIsDeleted);
            }

            if (result)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Abort;
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void companyComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading)
            {
                mCompanyID = (int)companyComboBox.SelectedValue;
            }
        }

        private void contractNameTextBox_TextChanged(object sender, EventArgs e)
        {
            mContractName = contractNameTextBox.Text.Trim();
        }

        private void contractDescriptionTextBox_TextChanged(object sender, EventArgs e)
        {
            mDescription = contractDescriptionTextBox.Text.Trim();
        }

        private void activeChekBox_CheckedChanged(object sender, EventArgs e)
        {
            mIsDeleted = !activeChekBox.Checked;
        }

    }
}
